package com.cvo.scrumtoolkit.client.pannels.widgets;

import java.util.LinkedList;
import java.util.List;

import com.cvo.scrumtoolkit.client.controller.ControllerImpl;
import com.cvo.scrumtoolkit.client.controller.Navigator;
import com.cvo.scrumtoolkit.client.controller.View;
import com.cvo.scrumtoolkit.client.entities.BacklogItem;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.cvo.scrumtoolkit.client.pannels.PersistentService;
import com.cvo.scrumtoolkit.client.pannels.PersistentServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class BacklogWidget extends Composite {
	private BacklogItem backupBacklogItem;
	private Project project;
	private Integer backupInteger;
	private BacklogItem backlogItem;
	private LinkedList<BacklogItem> backlogItems;
	private static BacklogWidgetUiBinder uiBinder = GWT	.create(BacklogWidgetUiBinder.class);
	private final PersistentServiceAsync persistentService = GWT.create(PersistentService.class);
	String id;
	
	interface BacklogWidgetUiBinder extends UiBinder<Widget, BacklogWidget> {
	}
	public BacklogWidget(final BacklogItem backlogItem,final int i) {
		initWidget(uiBinder.createAndBindUi(this));
		this.backlogItem= backlogItem;
		try {
			id =Navigator.getNavigator().getProjectId();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		
		
		//backlogItems.add(backlogItem);
		lblBacklogname.setText(this.backlogItem.getBacklogItemName());
		lblPrio.setTitle("Prioriteit");
		//lblPrio.setText(backlogItems.indexOf(backlogItem)+1+"");
		//Controle();
		check.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				
				persistentService.setSprintInBacklogitem(backlogItem.getBacklogItemName(), i, new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					

					@Override
					public void onSuccess(Void result) {
					System.out.println("Backlogitem: "+backlogItem.getBacklogItemName()+"zit nu in sprint: "+i);
						
					}
				});
				
				
			
				
				
				//check.setVisible(false);
			}
		});
		

	}
	
	public BacklogWidget(final BacklogItem backlogItem) {
		initWidget(uiBinder.createAndBindUi(this));
		this.backlogItem= backlogItem;
		try {
			id =Navigator.getNavigator().getProjectId();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		
		
		//backlogItems.add(backlogItem);
		lblBacklogname.setText(this.backlogItem.getBacklogItemName());
		lblPrio.setText("in sprint");
		buttonmin.setVisible(false);
		buttonplus.setVisible(false);
		check.setVisible(false);
	}
	
	
	public void Switch(int i){
		backupBacklogItem=backlogItem;
		backupInteger=backlogItems.indexOf(backlogItem)-i;
		backlogItems.set(backlogItems.indexOf(backlogItem), backlogItems.get(backlogItems.indexOf(backlogItem)-i));
		backlogItems.set(backupInteger, backupBacklogItem);
	}
	public void Controle(){
		if(backlogItems.getFirst()==backlogItem){
			//	if(backlogItems.size()-1==backlogItems.indexOf(backlogItem)){
			buttonplus.setEnabled(false);
		}
		if(backlogItems.getLast()==backlogItem){
			//	if(backlogItems.indexOf(backlogItem)==0){
			buttonmin.setEnabled(false);
			}
	}
	@UiField 
	CheckBox check;
	@UiField 
	Label lblBacklogname;
	
	@UiField
	Label lblPrio;
		
	@UiField
	Button buttonplus;
	
	@UiField
	Button buttonmin;
	
	@UiHandler("buttonplus")
	void onPlus(ClickEvent e) {
		Switch(1);
	}
	
	@UiHandler("buttonmin")
	void onMin(ClickEvent e) {
		Switch(-1);
	}
	
	
	
	

}
